package com.supermarket.controller;

import com.supermarket.common.ResponseResult;
import com.supermarket.entity.ArticleMedia;
import com.supermarket.exception.ArticleException;
import com.supermarket.service.ArticleMediaService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/article-media")
public class ArticleMediaController {

    @Resource
    private ArticleMediaService articleMediaService;

    /**
     * 上传文章媒体文件
     *
     * @param articleId 文章ID
     * @param file      媒体文件
     * @param mediaType 媒体类型
     * @param sortOrder 排序顺序
     * @return 包含媒体信息的响应结果
     */
    @PostMapping("/upload")
    public ResponseResult<ArticleMedia> uploadMedia(
            @RequestParam("articleId") Integer articleId,
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "mediaType", defaultValue = "IMAGE") String mediaType,
            @RequestParam(value = "sortOrder", defaultValue = "0") Integer sortOrder) {
        
        try {
            if (file.isEmpty()) {
                return ResponseResult.error(400, "文件不能为空");
            }
            
            ArticleMedia media = articleMediaService.uploadMedia(articleId, file, mediaType, sortOrder);
            return ResponseResult.success(media);
        } catch (ArticleException e) {
            return ResponseResult.error(400, e.getMessage());
        } catch (IOException e) {
            return ResponseResult.error(500, "文件上传失败: " + e.getMessage());
        }
    }

    /**
     * 更新媒体信息
     *
     * @param media 媒体对象
     * @return 包含操作结果的响应结果
     */
    @PutMapping("/update")
    public ResponseResult<String> updateMedia(@RequestBody ArticleMedia media) {
        try {
            if (media.getId() == null) {
                return ResponseResult.error(400, "媒体ID不能为空");
            }
            
            int result = articleMediaService.updateArticleMedia(media);
            if (result > 0) {
                return ResponseResult.success("媒体更新成功");
            } else {
                return ResponseResult.error(400, "媒体更新失败");
            }
        } catch (Exception e) {
            return ResponseResult.error(500, "更新失败: " + e.getMessage());
        }
    }

    /**
     * 更新媒体排序
     *
     * @param id        媒体ID
     * @param sortOrder 排序顺序
     * @return 包含操作结果的响应结果
     */
    @PutMapping("/sort/{id}")
    public ResponseResult<String> updateMediaSort(
            @PathVariable Integer id,
            @RequestParam("sortOrder") Integer sortOrder) {
        
        try {
            int result = articleMediaService.updateMediaSortOrder(id, sortOrder);
            if (result > 0) {
                return ResponseResult.success("排序更新成功");
            } else {
                return ResponseResult.error(400, "排序更新失败");
            }
        } catch (Exception e) {
            return ResponseResult.error(500, "更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除媒体
     *
     * @param id 媒体ID
     * @return 包含操作结果的响应结果
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> deleteMedia(@PathVariable Integer id) {
        try {
            int result = articleMediaService.deleteArticleMedia(id);
            if (result > 0) {
                return ResponseResult.success("媒体删除成功");
            } else {
                return ResponseResult.error(400, "媒体删除失败");
            }
        } catch (Exception e) {
            return ResponseResult.error(500, "删除失败: " + e.getMessage());
        }
    }

    /**
     * 获取媒体详情
     *
     * @param id 媒体ID
     * @return 包含媒体信息的响应结果
     */
    @GetMapping("/detail/{id}")
    public ResponseResult<ArticleMedia> getMediaDetail(@PathVariable Integer id) {
        ArticleMedia media = articleMediaService.getArticleMediaById(id);
        if (media != null) {
            return ResponseResult.success(media);
        } else {
            return ResponseResult.error(404, "媒体不存在");
        }
    }

    /**
     * 获取文章所有媒体
     *
     * @param articleId 文章ID
     * @return 包含媒体列表的响应结果
     */
    @GetMapping("/article/{articleId}")
    public ResponseResult<List<ArticleMedia>> getArticleMedia(@PathVariable Integer articleId) {
        List<ArticleMedia> mediaList = articleMediaService.getMediaByArticleId(articleId);
        return ResponseResult.success(mediaList);
    }

    /**
     * 根据文章ID和媒体类型获取媒体
     *
     * @param articleId 文章ID
     * @param mediaType 媒体类型
     * @return 包含媒体列表的响应结果
     */
    @GetMapping("/article/{articleId}/type/{mediaType}")
    public ResponseResult<List<ArticleMedia>> getArticleMediaByType(
            @PathVariable Integer articleId,
            @PathVariable String mediaType) {
        
        List<ArticleMedia> mediaList = articleMediaService.getMediaByArticleIdAndType(articleId, mediaType);
        return ResponseResult.success(mediaList);
    }

    /**
     * 统计媒体类型数量
     *
     * @param mediaType 媒体类型
     * @return 包含媒体数量的响应结果
     */
    @GetMapping("/count/{mediaType}")
    public ResponseResult<Integer> countMediaByType(@PathVariable String mediaType) {
        int count = articleMediaService.countMediaByType(mediaType);
        return ResponseResult.success(count);
    }
} 